package cn.mumzone.pos.presenter;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.model.ReserveTimeEntity;
import cn.mumzone.pos.model.source.ReserveRepository;
import cn.mumzone.pos.presenter.contract.UpdateReserveTimeContract;

/**
 * Created by qiuzhenhuan on 2019/7/6.
 */


public class UpdateReserveTimePresenter extends AbsPresenter<UpdateReserveTimeContract.View, ReserveRepository>
        implements UpdateReserveTimeContract.Presenter {


    @Override
    public void loadAvailableTime(String appointmentId, String date) {
        mView.showLoadingDialog();
        ApiCallBack<List<ReserveTimeEntity>> callBack = new ApiCallBack<List<ReserveTimeEntity>>() {
            @Override
            public void onSuccess(List<ReserveTimeEntity> list) {
                mView.dismissLoadingDialog();
                ArrayList<ReserveTimeEntity> morning = new ArrayList<>();
                ArrayList<ReserveTimeEntity> afternoon = new ArrayList<>();
                String firstEnableTime = "";
                for (int index = 0; index < list.size(); index++) {
                    ReserveTimeEntity reserveTimeEntity = list.get(index);
                    String minuteName = reserveTimeEntity.getMinuteName();
                    if (!StringUtils.isEmpty(minuteName)) {
                        String hour = minuteName.split(":")[0];
                        if (Integer.parseInt(hour) < 13) {
                            morning.add(reserveTimeEntity);
                        } else {
                            afternoon.add(reserveTimeEntity);
                        }
                    }
                    if (reserveTimeEntity.isEnable() && StringUtils.isEmpty(firstEnableTime)) {
                        firstEnableTime = reserveTimeEntity.getMinuteName();
                    }
                }
                mView.showAvailableTime(morning,afternoon);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showError(response.getMsg());
            }
        };
        mRepo.getUpdateTimeByOrderCode(appointmentId, date, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void doUpdateReserveTime(String appointmentId, final String date) {
        mView.showLoadingDialog();
        ApiCallBack<String> callBack = new ApiCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                mView.dismissLoadingDialog();
                mView.showUpdateReserveTimeSuccess(date);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showError(response.getMsg());
            }
        };
        mRepo.updateAppointmentTime(appointmentId, date, callBack);
        addSubscrebe(callBack);
    }
}
